//
//  ZYXClassNameVC.m
//  RuiBeiKang
//
//  Created by iOS开发1 on 2019/1/8.
//  Copyright © 2019年 JW. All rights reserved.
//

#import "ZYXClassNameVC.h"
#import "ZYXClassNameHeaderView.h"
#import "ZYXClassNameCell.h"
#import "ZYXClassNameModel.h"
#import "ZYXDownLoadVC.h"


@interface ZYXClassNameVC ()<ZYXClassNameHeaderDelegate>
///数据源
@property (strong, nonatomic) NSMutableArray *dataMArr;

@property (strong, nonatomic) NSIndexPath *indexPath;

@property (strong,nonatomic) UILabel *moneyLabel;

@property (strong,nonatomic) UILabel *actualLabel;



@end

@implementation ZYXClassNameVC

- (void)viewDidLoad {
    [super viewDidLoad];
//    [self.view addObserver:self forKeyPath:@"frame" options:NSKeyValueObservingOptionNew context:nil];
    // Do any additional setup after loading the view from its nib.
    self.title = @"目录";
}

//- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
//{
//    if ([keyPath isEqualToString:@"frame"]) {
//        [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.bottom.left.right.offset(0);
//            make.height.mas_equalTo(HEIGHT(48));
//        }];
//        [self.buyAtOnce mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.bottom.top.right.equalTo(self.bottomView);
//            make.bottom.width.mas_equalTo (HEIGHT(100));
//        }];
//    }
//}

- (void)setupViews {
    [self.tableView registerNib:[UINib nibWithNibName:@"ZYXClassNameCell" bundle:nil] forCellReuseIdentifier:@"ZYXClassNameCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"ZYXClassNameHeaderView" bundle:nil] forHeaderFooterViewReuseIdentifier:@"ZYXClassNameHeaderView"];
    self.tableView.estimatedRowHeight = aufont(26);
    self.tableView.separatorStyle  = UITableViewCellSeparatorStyleNone;
    self.tableView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-NavigationBar_Height-HEIGHT(48)-290-HomeIndicator_Height);
    
    
    self.downLoadBtn  = [UIButton buttonWithType:UIButtonTypeCustom];
    self.downLoadBtn.frame = CGRectMake(SCREEN_WIDTH-HEIGHT(65), 100, HEIGHT(50), HEIGHT(50));
    [self.downLoadBtn setBackgroundImage:[UIImage imageNamed:@"downLoad"] forState:UIControlStateNormal];
//    [self.downLoadBtn addTarget:self action:@selector(clickDownLoadBtn) forControlEvents:UIControlEventTouchUpInside ];
    [self.view addSubview:self.downLoadBtn];
    
    self.indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    
    self.bottomView = [[UIView alloc]init];
    self.bottomView.frame = CGRectMake(0, self.tableView.height, SCREEN_WIDTH, HEIGHT(48));
    [self.view addSubview:self.bottomView];
    self.bottomView.backgroundColor = [UIColor whiteColor];

    
    self.actualLabel = [[UILabel alloc]initWithFrame:CGRectMake(HEIGHT(15), 0, HEIGHT(65), HEIGHT(48))];
    self.actualLabel.text = @"实付金额:";
    self.actualLabel.font = FONT(15);
    self.actualLabel.textColor = RGB(102, 102, 102);
    [self.bottomView addSubview:self.actualLabel];
    
    self.moneyLabel = [[UILabel alloc]initWithFrame:CGRectMake(HEIGHT(80), 0, HEIGHT(150), HEIGHT(48))];
    self.moneyLabel.text = @"¥:250.00";
    self.moneyLabel.font = FONT(15);
    self.moneyLabel.textColor = RGB(240,53,34);
    [self.bottomView addSubview:self.moneyLabel];
    
    self.buyAtOnce = [UIButton buttonWithType:UIButtonTypeCustom];
//    [self.buyAtOnce setTitle:@"立即购买" forState:UIControlStateNormal];
    [self.buyAtOnce setImage:IMG(@"lijigoumai") forState:UIControlStateNormal];
    self.buyAtOnce.frame = CGRectMake(SCREEN_WIDTH-HEIGHT(130), 0, HEIGHT(130), HEIGHT(48));
    [self.bottomView addSubview:self.buyAtOnce];

    
}

- (BOOL)emptyDataSetShouldDisplay:(UIScrollView *)scrollView {
    if (self.dataMArr.count > 0) {
        return NO;
    }
    return YES;
}
///下载
-(void)clickDownLoadBtn{
    ZYXDownLoadVC *vc = [[ZYXDownLoadVC alloc]init];
    vc.zid = self.zid;
    [self.navigationController pushViewController:vc animated:YES];
}
-(void)loadData{
    if ([UID integerValue] ==0) {

        return;
    }
    
    NSLog(@"%@",@{@"zid":self.zid,@"user_id":UID});
    
    [NetWork POSTWithUrlString:kInterfaceAllUrl(PATH_FenleiThree) parmas:@{@"zid":self.zid,@"user_id":UID} successBloock:^(NSDictionary *responseObject) {
        NSLog(@"responseObject =%@",responseObject);
        if ([responseObject[@"code"] isEqualToNumber:@200]) {
            
            NSArray *modelArr = [ZYXClassNameModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"]];
            self.dataMArr = [NSMutableArray arrayWithArray:modelArr];
            NSInteger isYes = 0;
            if (self.dataMArr.count>0) {
                for (ZYXClassNameModel *model in self.dataMArr) {
                    if (isYes == 0) {
                        model.isTop = YES;
                    }else{
                        model.isTop = NO;
                    }
                    isYes = isYes+1;
                }
                ZYXClassNameModel *model = self.dataMArr[self.indexPath.section];
                if (model.res.count >0) {
                    ZYXClassNameDerailModel *detModel= model.res[self.indexPath.row];
                    if ([detModel.free isEqualToString:@"1"]) {
                        
                        [self.delegate clickClassNameModel:detModel];
                    }else{
                         [JKToast showWithText:@"该课程需要购买才能观看"];
                    }
             
                }
    
            }
            
            
            ZYXClassModel *classModel  = [ZYXClassModel mj_objectWithKeyValues:responseObject];
            
            
            //is_vip是否会员 1:是  2：不是   is_buy是否购买 1：是 2：不是
            if ([classModel.is_vip isEqualToString:@"1"] || [classModel.is_buy isEqualToString:@"1"]) {
                self.tableView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-NavigationBar_Height-290-HomeIndicator_Height);
        
                self.bottomView.hidden = YES;
           
                
            }else{
                self.tableView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-NavigationBar_Height-290-HomeIndicator_Height-HEIGHT(48));
                self.bottomView.hidden = NO;
          
                //判断价格
                self.moneyLabel.text = [NSString stringWithFormat:@"￥%@",classModel.price];
            }
        
        
            [self.tableView reloadData];
        }else{
            [JKToast showWithText:responseObject[@"message"]];
        }
        
    } failureBlock:^(NSError *error) {
        
    }];
    

    
//    ZYXClassNameDerailModel *model0_0 = [[ZYXClassNameDerailModel alloc]initWitTitle:@"1-1 什么是类和对象" andVideo_url:@"10000" andVid:@""  isSelect:NO];
//    ZYXClassNameDerailModel *model0_1 = [[ZYXClassNameDerailModel alloc]initWitTitle:@"1-2 Java中的static使用之静态初始...Java中的static使用之静态初始...Java中的static使用之静态初始..." andVideo_url:@"" andVid:@""isSelect:NO];
//    ZYXClassNameDerailModel *model0_2 = [[ZYXClassNameDerailModel alloc]initWitTitle:@"1-3 如何定义Java中的类" andVideo_url:@"" andVid:@"" isSelect:NO];
//    ZYXClassNameModel *model0 = [[ZYXClassNameModel alloc]initWitTitle:@"第一章 基本工具" andNSArry:@[model0_0,model0_1] isMore:NO];
//    ZYXClassNameModel *model1 = [[ZYXClassNameModel alloc]initWitTitle:@"第二章 Java中的封装" andNSArry:@[model0_0,model0_1,model0_0] isMore:NO];
//    ZYXClassNameModel *model2 = [[ZYXClassNameModel alloc]initWitTitle:@"第三章 Java中的继承第三章 Java中的继承第三章 Java中的继承" andNSArry:@[model0_0,model0_1,model0_2] isMore:NO];
//    self.dataMArr = [NSMutableArray arrayWithObjects:model0,model1,model2, nil];
    
}
#pragma mark UITableViewDelegate,UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataMArr.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    ZYXClassNameModel *model = self.dataMArr[section];
    if (model.isTop) {
        return model.res.count;
    }else{
        return 0;
    }
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ZYXClassNameModel *model = self.dataMArr[indexPath.section];
    ZYXClassNameDerailModel *detModel= model.res[indexPath.row];
    if ([self.indexPath isEqual:indexPath]) {
        if ([detModel.free isEqualToString:@"1"]) {
            detModel.isSelect = YES;
        }else{
                detModel.isSelect = NO;
        }
    }else{
        detModel.isSelect = NO;
    }
    ZYXClassNameCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ZYXClassNameCell"];
    cell.indexPath = indexPath;
    cell.model = detModel;
    
    return cell;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    ZYXClassNameModel *model = self.dataMArr[section];
    ZYXClassNameHeaderView *view = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"ZYXClassNameHeaderView"];
    view.delegate = self;
    view.model = model;
    view.indexPath = section;
    return view;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    self.indexPath = indexPath;
    ZYXClassNameModel *model = self.dataMArr[indexPath.section];
    ZYXClassNameDerailModel *detModel= model.res[indexPath.row];
    if ([detModel.free isEqualToString:@"1"]) {
        detModel.isSelect = YES;
        [self.tableView reloadData];
        [self.delegate clickClassNameModel:detModel];
    }else{
           [JKToast showWithText:@"该课程需要购买才能观看"];
    }

    
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 1)];
    view.backgroundColor = RGB(230, 230, 230);
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return aufont(40);
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    ZYXClassNameModel *model = self.dataMArr[section];
    if (model.isTop) {
        return 1;
    }else{
        return CGFLOAT_MIN;
    }
    
}
#pragma mark ZYXClassNameHeaderView
-(void)didClickTopBtn:(NSInteger)tag andView:(ZYXClassNameHeaderView *)view{
    ZYXClassNameModel *model = self.dataMArr[tag];
    model.isTop = !model.isTop;
    [self.tableView reloadData];
    
}
///立即购买
- (IBAction)clickBuyBtn:(UIButton *)sender {
    
}
#pragma mark 懒加载
- (NSMutableArray *)dataMArr{
    if (!_dataMArr) {
        _dataMArr = [NSMutableArray array];
        
    }
    return _dataMArr;
}
@end
